package com.ts.api.http.org.biz;


import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.algorithm.digest.MD5;
import com.gitee.apanlh.util.algorithm.encrypt.symmetric.AES;
import com.gitee.apanlh.util.base.ChooseEq;
import com.gitee.apanlh.util.base.CollUtils;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.base.MapUtils;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.ts.api.common.util.H5DomainUrlUtils;
import com.ts.api.http.org.entity.apply.OrgApplyMaskApplyF;
import com.ts.api.http.org.entity.dataexchange.ApiOrgMaskDataExchangeF;
import com.ts.api.module.api.convert.core.annotation.ApiPlan;
import com.ts.api.module.api.convert.core.annotation.ApiPlanConvertType;
import com.ts.api.module.api.convert.core.entity.ApiApplyParameter;
import com.ts.api.module.api.convert.core.entity.ApiDataExchangeParameter;
import com.ts.api.module.api.convert.org.handler.ApiOrgPhoneMaskConvertHandler;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMaskApply;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMaskDataExchange;

import java.util.Map;


/**
 * 10030F
 * @ Author 江小游
 * @ Date 2025/6/13 17:42
 */
@ApiPlan(productId = "227", type = ApiPlanConvertType.ORG_PHONE_MASK)
public class ApiOrgPhoneMaskF implements ApiOrgPhoneMaskConvertHandler {
    private static final String CHANNEL_CODE = "NDmctOXq";
    private static final String KEY = "AbFIviAn3RGAuSEl";

    @Override
    public Object dataExchange(ApiDataExchangeParameter apiDataChangeMessage) throws Exception {
        //  撞库流程
        ApiOrgMaskDataExchangeF apiOrgDataExchange = new ApiOrgMaskDataExchangeF();
        com.ts.api.module.api.convert.core.utils.ApiFieldConverter apiFieldConverter = com.ts.api.module.api.convert.core.utils.ApiFieldConverter.create(apiDataChangeMessage);
        // 姓名
        apiOrgDataExchange.setNameMd5(MD5.create().digestToBase64Str(apiDataChangeMessage.getUserName()));
        apiOrgDataExchange.setMobileMd5(apiDataChangeMessage.getPhoneMd5());
        apiOrgDataExchange.setMobilePart(apiDataChangeMessage.getPhoneMask());
        apiOrgDataExchange.setIdCard(apiDataChangeMessage.getIdcard());
        apiOrgDataExchange.setCity(apiDataChangeMessage.getCity());
        // 姓名
        apiOrgDataExchange.setAge(apiFieldConverter.getAgeInt());
        //  性别 0-女 1-男
        Integer sex = ChooseEq.create(apiDataChangeMessage.getSex())
                .when("0", 1)
                .when("1", 0)
                .end();
        apiOrgDataExchange.setSex(sex);
        /*
           职业
            1-上班族
            2-公务员/事业单位
            3-私营业主
            4-个体户
            5-其他
            6-国企员工
            7-自由职业
            8-媒体从业者
            9-司法人员
            10-金融从业者
            11-在校学生
         */
        String career = ChooseEq.create(apiDataChangeMessage.getCareer())
                .when("1", "1")
                .when("2", "7")
                .when("3", "3")
                .when("4", "2")
                .when("5", "4")
                .end();
        apiOrgDataExchange.setOccupation(career);
        /*
          学历
          0-大专及以下
          1-本科
          2-硕士及以上
        */
        Integer educationAttainment = ChooseEq.create(apiDataChangeMessage.getEducationAttainment())
                .when("1", 0)
                .when("2", 0)
                .when("3", 0)
                .when("4", 0)
                .when("5", 1)
                .when("6", 2)
                .end();
        apiOrgDataExchange.setEducation(educationAttainment);
        /*
          月收入
          0-8千及以下
          1-8千至1.5万
          2-1.5万至3万
          3-3万以上
        */
        Integer monthlySalary = ChooseEq.create(apiDataChangeMessage.getMonthlySalary())
                .when("1", 0)
                .when("2", 0)
                .when("3", 1)
                .when("4", 2)
                .when("5", 3)
                .end();
        apiOrgDataExchange.setMonthIncome(monthlySalary);
        /*
            花呗额度
            0-无
            1-5千以下
            2-5千以上
         */
        Integer hbLimit = ChooseEq.create(apiDataChangeMessage.getHbLimit())
                .when("1", 0)
                .when("2", 1)
                .when("3", 2)
                .when("4", 2)
                .when("5", 2)
                .end();
        apiOrgDataExchange.setHuabei(hbLimit);
        /*
           白条额度
           0-无
           1-5千以下
           2-5千以上
         */
        Integer btLimit = ChooseEq.create(apiDataChangeMessage.getBtLimit())
                .when("1", 0)
                .when("2", 1)
                .when("3", 2)
                .when("4", 2)
                .when("5", 2)
                .end();
        apiOrgDataExchange.setWhiteStripe(btLimit);
        /*
           贷款金额
           0-2千到5万
           1-5万到10万
           2-10万到20万
           3-20万以上
         */
        Integer loanAmount = ChooseEq.create(apiDataChangeMessage.getLoanAmount())
                .when("1", 0)
                .when("2", 1)
                .when("3", 2)
                .when("4", 4)
                .end();
        apiOrgDataExchange.setLoanAmount2(loanAmount);
        /*
        借款期限
        00001-3个月
        00002-6个月
        00003-12个月
        00004-24个月
        00005-36个月
        00006-48个月
         */
        Integer loanTerm = ChooseEq.create(apiDataChangeMessage.getLoanTerm())
                .when("1", 00001)
                .when("2", 00002)
                .when("3", 00003)
                .when("4", 00004)
                .when("5", 00005)
                .when("6", 00006)
                .end();
        apiOrgDataExchange.setLoanTerm(loanTerm);
        /*
          公积金
         0-无公积金
         1-未满6个月
         2-6个月以上
        */
        Integer providentFund = ChooseEq.create(apiDataChangeMessage.getProvidentFund())
                .when("1", 0)
                .when("2", 1)
                .when("3", 2)
                .when("4", 1)
                .end();
        apiOrgDataExchange.setPublicAccumulationFunds(providentFund);
        /*
          信用卡额度
          0-无信用卡
          1-有信用卡
        */
        Integer creditLimit = ChooseEq.create(apiDataChangeMessage.getCreditLimit())
                .when("1", 0)
                .when("2", 1)
                .when("3", 1)
                .when("4", 1)
                .when("5", 1)
                .when("6", 1)
                .end();
        apiOrgDataExchange.setCreditCard(creditLimit);
        /*
          房产情况
          0-有
          1-无
        */
        Integer realEstateInfo = ChooseEq.create(apiDataChangeMessage.getRealEstateInfo())
                .when("1", 1)
                .when("2", 0)
                .when("3", 0)
                .when("4", 0)
                .when("5", 0)
                .when("6", 0)
                .end();
        apiOrgDataExchange.setHouseProperty(realEstateInfo);
        /*
          社保缴纳
          0-无社保
          1-未满6个月
          2-6个月以上
        */
        Integer socialSecurity = ChooseEq.create(apiDataChangeMessage.getSocialSecurity())
                .when("1", 0)
                .when("2", 1)
                .when("3", 2)
                .when("4", 1)
                .end();
        apiOrgDataExchange.setSocialInsurance(socialSecurity);
        // 寿险
        apiOrgDataExchange.setInsurancePolicy(0);
        /*
           车辆情况
           0-有
           1-无
         */
        Integer carInfo = ChooseEq.create(apiDataChangeMessage.getCarInfo())
                .when("1", 1)
                .when("2", 0)
                .when("3", 0)
                .when("4", 0)
                .end();
        apiOrgDataExchange.setCar(carInfo);
        /*
           芝麻分
           1.600分以下
           2.600~649分
           3.650~699分
           4.700分以上
         */
        Integer sesameCredit = ChooseEq.create(apiDataChangeMessage.getSesameCredit())
                .when("1", 1)
                .when("2", 2)
                .when("3", 3)
                .when("4", 4)
                .end();
        apiOrgDataExchange.setZhimaScore(sesameCredit);
        // 微粒贷
        apiOrgDataExchange.setVliLoan(0);
        /*
          信用情况（逾期）
           0-正常
           1-逾期
         */
        Integer overdue = ChooseEq.create(apiDataChangeMessage.getOverdue())
                .when("1", 0)
                .when("2", 1)
                .when("3", 1)
                .end();
        apiOrgDataExchange.setCreditInstance(overdue);
        // 债务情况
        apiOrgDataExchange.setDebtInstance(0);
        // 申请ip
        apiOrgDataExchange.setIp(apiDataChangeMessage.getIp());
        //  加密
        AES aes = AES.createEcb(KEY.getBytes());
        String data = aes.encryptToBase64Str(JsonUtils.toJson(apiOrgDataExchange));
        Map<String, Object> map = MapUtils.newHashMap(newMap -> {
            newMap.put("data", data);
            newMap.put("channelCode", CHANNEL_CODE);
        });
        return JsonUtils.toJson(map);
    }

    @Override
    public RespOrgPhoneMaskDataExchange respDataExchange(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMaskDataExchange.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 200)) {
            return RespOrgPhoneMaskDataExchange.ofFail();
        }
        return RespOrgPhoneMaskDataExchange.ofSuc();
    }

    @Override
    public Object apply(ApiApplyParameter apiApplyMessage) throws Exception {
        //  进件流程
        OrgApplyMaskApplyF apiOrgApply = new OrgApplyMaskApplyF();
        com.ts.api.module.api.convert.core.utils.ApiFieldConverter apiFieldConverter = com.ts.api.module.api.convert.core.utils.ApiFieldConverter.create(apiApplyMessage);
        //  赋值进件属性
        apiOrgApply.setName(apiApplyMessage.getUserName());
        apiOrgApply.setMobile(apiApplyMessage.getPhone());
        apiOrgApply.setMobilePart(apiApplyMessage.getPhoneMask());
        apiOrgApply.setCheckNo(apiApplyMessage.dataExchangeApplyId());
        apiOrgApply.setIdCard(apiApplyMessage.getIdcard());
        //  年龄
        apiOrgApply.setAge(apiFieldConverter.getAgeInt());
        //  城市
        apiOrgApply.setCity(apiApplyMessage.getCity());
        //  性别 0-女 1-男
        Integer sex = ChooseEq.create(apiApplyMessage.getSex())
                .when("0", 1)
                .when("1", 0)
                .end();
        apiOrgApply.setSex(sex);
        /*
           职业
            1-上班族
            2-公务员/事业单位
            3-私营业主
            4-个体户
            5-其他
            6-国企员工
            7-自由职业
            8-媒体从业者
            9-司法人员
            10-金融从业者
            11-在校学生
         */
        String career = ChooseEq.create(apiApplyMessage.getCareer())
                .when("1", "1")
                .when("2", "7")
                .when("3", "3")
                .when("4", "2")
                .when("5", "4")
                .end();
        apiOrgApply.setOccupation(career);
        /*
          学历
          0-大专及以下
          1-本科
          2-硕士及以上
        */
        Integer educationAttainment = ChooseEq.create(apiApplyMessage.getEducationAttainment())
                .when("1", 0)
                .when("2", 0)
                .when("3", 0)
                .when("4", 0)
                .when("5", 1)
                .when("6", 2)
                .end();
        apiOrgApply.setEducation(educationAttainment);
        /*
          月收入
          0-8千及以下
          1-8千至1.5万
          2-1.5万至3万
          3-3万以上
        */
        Integer monthlySalary = ChooseEq.create(apiApplyMessage.getMonthlySalary())
                .when("1", 0)
                .when("2", 0)
                .when("3", 1)
                .when("4", 2)
                .when("5", 3)
                .end();
        apiOrgApply.setMonthIncome(monthlySalary);
        /*
            花呗额度
            0-无
            1-5千以下
            2-5千以上
         */
        Integer hbLimit = ChooseEq.create(apiApplyMessage.getHbLimit())
                .when("1", 0)
                .when("2", 1)
                .when("3", 2)
                .when("4", 2)
                .when("5", 2)
                .end();
        apiOrgApply.setHuabei(hbLimit);
        /*
           白条额度
           0-无
           1-5千以下
           2-5千以上
         */
        Integer btLimit = ChooseEq.create(apiApplyMessage.getBtLimit())
                .when("1", 0)
                .when("2", 1)
                .when("3", 2)
                .when("4", 2)
                .when("5", 2)
                .end();
        apiOrgApply.setWhiteStripe(btLimit);
        /*
           贷款金额
           0-2千到5万
           1-5万到10万
           2-10万到20万
           3-20万以上
         */
        Integer loanAmount = ChooseEq.create(apiApplyMessage.getLoanAmount())
                .when("1", 0)
                .when("2", 1)
                .when("3", 2)
                .when("4", 4)
                .end();
        apiOrgApply.setLoanAmount2(loanAmount);
        /* 借款期限
        00001-3个月
        00002-6个月
        00003-12个月
        00004-24个月
        00005-36个月
        00006-48个月
        */
        Integer loanTerm = ChooseEq.create(apiApplyMessage.getLoanTerm())
                .when("1", 00001)
                .when("2", 00002)
                .when("3", 00003)
                .when("4", 00004)
                .when("5", 00005)
                .when("6", 00006)
                .end();
        apiOrgApply.setLoanTerm(loanTerm);
        /*
          公积金
             0-无公积金
             1-未满6个月
             2-6个月以上
        */
        Integer providentFund = ChooseEq.create(apiApplyMessage.getProvidentFund())
                .when("1", 0)
                .when("2", 1)
                .when("3", 2)
                .when("4", 1)
                .end();
        apiOrgApply.setPublicAccumulationFunds(providentFund);
        /*
          信用卡额度
          0-无信用卡
          1-有信用卡
        */
        Integer creditLimit = ChooseEq.create(apiApplyMessage.getCreditLimit())
                .when("1", 0)
                .when("2", 1)
                .when("3", 1)
                .when("4", 1)
                .when("5", 1)
                .when("6", 1)
                .end();
        apiOrgApply.setCreditCard(creditLimit);
        /*
          房产情况
          0-有
          1-无
        */
        Integer realEstateInfo = ChooseEq.create(apiApplyMessage.getRealEstateInfo())
                .when("1", 1)
                .when("2", 0)
                .when("3", 0)
                .when("4", 0)
                .when("5", 0)
                .when("6", 0)
                .end();
        apiOrgApply.setHouseProperty(realEstateInfo);
        /*
          社保缴纳
          0-无社保
          1-未满6个月
          2-6个月以上
        */
        Integer socialSecurity = ChooseEq.create(apiApplyMessage.getSocialSecurity())
                .when("1", 0)
                .when("2", 1)
                .when("3", 2)
                .when("4", 1)
                .end();
        apiOrgApply.setSocialInsurance(socialSecurity);
        // 寿险
        apiOrgApply.setInsurancePolicy(0);
        /*
           车辆情况
           0-有
           1-无
         */
        Integer carInfo = ChooseEq.create(apiApplyMessage.getCarInfo())
                .when("1", 1)
                .when("2", 0)
                .when("3", 0)
                .when("4", 0)
                .end();
        apiOrgApply.setCar(carInfo);
        /*
           芝麻分
           1.600分以下
           2.600~649分
           3.650~699分
           4.700分以上
         */
        Integer sesameCredit = ChooseEq.create(apiApplyMessage.getSesameCredit())
                .when("1", 1)
                .when("2", 2)
                .when("3", 3)
                .when("4", 4)
                .end();
        apiOrgApply.setZhimaScore(sesameCredit);
        // 微粒贷
        apiOrgApply.setVliLoan(0);
        /*
          信用情况（逾期）
           0-正常
           1-逾期
         */
        Integer overdue = ChooseEq.create(apiApplyMessage.getOverdue())
                .when("1", 0)
                .when("2", 1)
                .when("3", 1)
                .end();
        apiOrgApply.setCreditInstance(overdue);
        // 申请ip
        apiOrgApply.setIp(apiApplyMessage.getIp());
        // 债务情况
        apiOrgApply.setDebtInstance(0);
        // 协议列表
        apiOrgApply.setAgreements(CollUtils.newArrayList(newList -> {
            OrgApplyMaskApplyF.Agreement protocolList = new OrgApplyMaskApplyF.Agreement();
            protocolList.setName("个人信息授权协议");
            protocolList.setUrl(H5DomainUrlUtils.createUserProtocolUrl(apiApplyMessage.getProductPlanId(), apiApplyMessage.getPhoneMd5()));
            newList.add(protocolList);
        }));
        //  加密
        AES aes = AES.createEcb(KEY.getBytes());
        String data = aes.encryptToBase64Str(JsonUtils.toJson(apiOrgApply));
        Map<String, Object> map = MapUtils.newHashMap(newMap -> {
            newMap.put("data", data);
            newMap.put("channelCode", CHANNEL_CODE);
        });
        return JsonUtils.toJson(map);
    }

    @Override
    public RespOrgPhoneMaskApply respApply(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMaskApply.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 200)) {
            return RespOrgPhoneMaskApply.ofFail();
        }
        return RespOrgPhoneMaskApply.ofSuc();
    }
}

